package com.pan.alg.leecode;

import com.pan.util.PrintUtil;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2021/7/13
 */
public class Leecode80 {

    public static int removeDuplicates(int[] nums) {

        int index=0;
        int count=0;
        for (int i = 1; i < nums.length; i++) {

            if(nums[i]!=nums[i-1]){
                index++;
                nums[index]=nums[i];
                count=0;
            }
            if(nums[i]==nums[i-1]){
                count++;
                if(count<2){
                    index++;
                }
            }
        }
        return index;
    }

    public static int removeDuplicatesOfficial(int[] nums) {

        int n = nums.length;
        if(n<=2){
            return n;
        }

        int slow=2,fast =2;
        while(fast<n){
            if(nums[slow-2]!=nums[fast]){
                nums[slow]=nums[fast];
                slow++;
            }
            fast++;
        }
        return fast;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{0,0,1,1,1,1,2,3,3};
        int index = removeDuplicates(nums);
        System.out.println(index);
        PrintUtil.printArray(nums);
    }

}
